package com.l024.hi_device_util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.l024.hi_device_util.applist.AppListUtil;
import com.l024.hi_device_util.contact.ContactModel;
import com.l024.hi_device_util.contact.ContactUtil;
import com.l024.hi_device_util.device.DeviceModel;
import com.l024.hi_device_util.device.DeviceUtil;
import com.l024.hi_device_util.message.MessageModel;
import com.l024.hi_device_util.message.MessageUtil;

import java.util.List;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

/** HiDeviceUtilPlugin */
public class HiDeviceUtilPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

  private MethodChannel channel;
  private Context context;
  private Activity activity;
  private Result deviceResult;
  private Result contactResult;
  private Result messageResult;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "hi_device_util");
    channel.setMethodCallHandler(this);
    context = flutterPluginBinding.getApplicationContext();

  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if(call.method.equals("getDeviceInfo")){
      this.deviceResult = result;
      getDeviceInfo();
    }else if(call.method.equals("getContacts")){
      this.contactResult = result;
      getContacts();
    }else if(call.method.equals("getMessages")){
      this.messageResult = result;
      getMessages();
    } else if(call.method.equals("getAppList")){
      List<PackageInfo> appInfos = new AppListUtil().getAppInfos(context);
      result.success(JSONObject.toJSON(appInfos));
    }else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();
    binding.addRequestPermissionsResultListener(new PluginRegistry.RequestPermissionsResultListener() {
      @Override
      public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
          case DeviceUtil.DEVICE_CODE:
            getDeviceInfo();
            break;
          case ContactUtil.CONTACT_CODE:
            getContacts();
            break;
          case MessageUtil.MESSAGE_CODE:{
            getMessages();
            break;
          }
        }
        return false;
      }
    });
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }

  /**
   * 获取设备信息
   */
  private void getDeviceInfo(){
    if (DeviceUtil.getInstance().checkPermission(activity)){
      DeviceUtil.getInstance().getDeviceInfo(context,onGaidListener);
    }else {
      //申请权限
      deviceResult.error("500","没有权限","");
      //ActivityCompat.requestPermissions(activity,new String[]{DeviceUtil.permission},DeviceUtil.DEVICE_CODE);
    }
  }

  /**
   * 获取联系人发送
   */
  private void getContacts(){
    ContactUtil contactUtil = new ContactUtil();
    if (contactUtil.checkPermission(activity)){
      //获取联系人
      List<ContactModel> contacts = contactUtil.getContacts(activity);
      System.out.println("联系人信息:"+contacts);
      contactResult.success(JSONObject.toJSON(contacts));
    }else {
      contactResult.error("500","没有权限","");
      //申请权限
      //ActivityCompat.requestPermissions(activity,new String[]{ContactUtil.permission},ContactUtil.CONTACT_CODE);
    }
  }

  /**
   * 获取短信送
   */
  private void getMessages(){
    MessageUtil messageUtil = new MessageUtil();
    if (messageUtil.checkPermission(activity)){
      //获取联系人
      List<MessageModel> messages = messageUtil.getMessages(activity);
      System.out.println("短信信息:"+messages);
      messageResult.success(JSONObject.toJSON(messages));
    }else {
      messageResult.error("500","没有权限","");

      //申请权限
      //ActivityCompat.requestPermissions(activity,new String[]{MessageUtil.permission},MessageUtil.MESSAGE_CODE);
    }
  }

  private DeviceUtil.OnGaidListener onGaidListener = new DeviceUtil.OnGaidListener() {
    @Override
    public void onSuccess(DeviceModel deviceModel) {
      System.out.println("设备信息-->"+deviceModel.toString());
      deviceResult.success(JSONObject.toJSON(deviceModel));
    }
  };
}

